<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>微信登录回调</title>
    <!-- 引入 jQuery -->
    <script src="../templates/h5/assets/jquery.min.js"></script>
</head>
<body>
<script>
    // 从URL中提取code参数（使用jQuery辅助解析）
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        return r ? decodeURIComponent(r[2]) : null;
    }

    var code = getUrlParam("code");
    if (code) {
        // 将code传给后端，完成登录
        $.post("/api/wechat/login", { code: code }, function(result) {
            if (result.code === 200) {
                // 登录成功，保存token到localStorage，跳转首页
                localStorage.setItem("token", result.data.token);
                alert("登录成功！");
                window.location.href = "/index.html"; // 跳转至首页
            } else {
                alert("登录失败：" + result.msg);
            }
        }, "json").fail(function(xhr) {
            alert("接口调用失败：" + xhr.responseText);
        });
    } else {
        alert("微信授权失败，未获取到code");
    }
</script>
</body>
</html>